package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.UserDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.User;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-08-26 14:00
 */
public class UserDaoImpl implements UserDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    private int row;

    @Override
    public User getUserByIdByPass(String userId, String password) {
        User user = null;
        String sql = "select * from user where userId =?and password=?";
        try {
            //获取连接
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, userId);
            pst.setString(2,password);
            rs = pst.executeQuery();
            while(rs.next()) {
                user = new User();
                user.setUserId(rs.getString("userId"));
                user.setPassword(rs.getString("password"));
                user.setUserName(rs.getString("userName"));
                user.setUserSex(rs.getInt("userSex"));
                user.setUserImg(rs.getString("UserImg"));
                user.setDelTag(rs.getInt("delTag"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs,pst);
        }
        return user;
    }


    @Override
    public int getUserById(String userId) {
        User user=null;
        String sql="select * from user where userId=?";
        try{
            //获取连接
            con= DBUtil.getConnection();
            pst=con.prepareStatement(sql);

            pst.setString(1,userId);
            rs= pst.executeQuery();
            while(rs.next()) {
                user = new User();
                user.setUserId(rs.getString("userId"));
                user.setPassword(rs.getString("password"));
                user.setUserName(rs.getString("userName"));
                user.setUserSex(rs.getInt("userSex"));
                user.setUserImg(rs.getString("UserImg"));
                user.setDelTag(rs.getInt("delTag"));
            }
            if(rs.next()){
                return 1;
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DBUtil.close(rs,pst);
        }
        return 0;
    }

    @Override
    public int saveUser(String userId, String password, String userName, int userSex) {
        User user=null;
        String sql = "insert into user (userId,password,userName,userSex)VALUES(?,?,?,?)";
        try{

            //获取连接
            Connection conn = DBUtil.getConnection();
            pst=conn.prepareStatement(sql);
            pst.setString(1, userId);
            pst.setString(2,password);
            pst.setString(3,userName);
            pst.setInt(4,userSex);

            row = pst.executeUpdate();
//            while(rs.next()) {
//                user = new User();
//                user.setUserId(rs.getString("userId"));
//                user.setPassword(rs.getString("password"));
//                user.setUserName(rs.getString("userName"));
//                user.setUserSex(rs.getInt("userSex"));
//            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DBUtil.close(rs,pst);
        }


//        if(user!=null){
//            return 1;
//        }
        return row;
    }
}
